package heven.holt.wan.android.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import heven.holt.wan.android.databinding.ItemNavigationLinkMenuBinding
import heven.holt.wan.android.model.NavigationModel

class NavigationLinkMenuAdapter(
    private val navigationList: List<NavigationModel>,
    private var currentPosition: Int
) :
    RecyclerView.Adapter<NavigationLinkMenuAdapter.NavigationLinkMenuViewHolder>() {

    var onItemClickListener: ((Int, Int) -> Unit)? = null

    inner class NavigationLinkMenuViewHolder(
        val binding: ItemNavigationLinkMenuBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        init {
            binding.root.setOnClickListener {
                onItemClickListener?.invoke(currentPosition, layoutPosition)
                currentPosition = layoutPosition
            }
        }
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): NavigationLinkMenuViewHolder {
        return NavigationLinkMenuViewHolder(
            ItemNavigationLinkMenuBinding.inflate(
                LayoutInflater.from(
                    parent.context
                ), parent, false
            )
        )
    }

    override fun onBindViewHolder(holder: NavigationLinkMenuViewHolder, position: Int) {
        holder.binding.navigationModel = navigationList[position]
        holder.binding.executePendingBindings()
    }

    override fun getItemCount(): Int = navigationList.size
}